package com.kcsj.service.user;

import com.kcsj.dao.User.ResourceDao;
import com.kcsj.domain.user.Resource;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
public class ResourceService {
    private static final ResourceDao resourceDao
            = ResourceDao.getInstance();
    private static final ResourceService resourceService
            =new ResourceService();
    private ResourceService(){}

    public static ResourceService getInstance(){
        return resourceService;
    }


    public Collection<Resource> findAll() throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<Resource> resources = resourceDao.findAll(conn);
        JdbcHelper.close(conn);
        return resources;
    }

    public int add(Resource resource) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        int id = resourceDao.add(resource, conn);
        JdbcHelper.close(conn);
        return id;
    }

    public boolean update(Resource resource) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean update = resourceDao.update(resource, conn);
        JdbcHelper.close(conn);
        return update;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean delete = resourceDao.delete(id, conn);
        JdbcHelper.close(conn);
        return delete;
    }
}
